#!/bin/bash

usage()
{
   echo "usage: rename.sh file <pattern 1 [pattern 2] [...]>"
}

if [ $# -lt 2 ]; then
   usage
   exit 1
fi

ORG=$1
shift
NAME=${ORG%.*}
SUFF=""

if [ ${#NAME} -ne ${#ORG} ]; then
	SUFF=${ORG##*.}
fi

NEW_NAME=${NAME}

while [ $# -ge 1 ]
do
  PAT=$1
  NEW_NAME=$(echo "${NEW_NAME}" | sed ''"$PAT"'')
  shift
done

if [ ${NEW_NAME} = ${NAME} ]; then
  echo "No name changes, ignore. "
  exit
fi

if [ -n "$SUFF" ]; then
	NEW_NAME=${NEW_NAME}.${SUFF}
fi

echo "Renaming: ${ORG} ===> ${NEW_NAME} ..."
mv -v ${ORG} ${NEW_NAME}
